//
//  WlisADVC.swift
//  Swift_missScarlett
//
//  Created by xmg on 16/8/12.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

import MJExtension
import SDWebImage
import Alamofire

let code2 = "phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

let WlisScreenH = UIScreen.mainScreen().bounds.size.height
let WlisScreenW = UIScreen.mainScreen().bounds.size.width
let iphone7 = (WlisScreenH == 736)
let iphone6 = (WlisScreenH == 667)
let iphone5 = (WlisScreenH == 568)
let iphone4 = (WlisScreenH == 480)

class WlisADVC: UIViewController {

    /** 按钮 */
    @IBOutlet weak var jumpBtn: UIButton!
    /** 占位View */
    @IBOutlet weak var adContainView: UIView!
    /** 图片 */
    @IBOutlet weak var launchImage: UIImageView!
    /** 模型数据 */
    var item:WlisAdItem?
    /** 定时器 */
    var timer:NSTimer?
    
    
    /** 懒加载广告视图 */
    lazy var launchImageView : UIImageView = {
        let imageV = UIImageView();
        let tap = UITapGestureRecognizer(target: self, action: "tap")
        imageV.addGestureRecognizer(tap);
        imageV.userInteractionEnabled = true;
        self.adContainView.addSubview(imageV)
        return imageV;
    }();
    override func viewDidLoad() {
        super.viewDidLoad()

        /** 设置启动图片 */
        self.setUpLaunchImage();
        /** 设置数据 */
        self.loadAdData();
        /** 定时器 */
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timeChange", userInfo: nil, repeats: true);
     
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

    /** 设置跳转页面 */
    @IBAction func jumpBtn(sender: AnyObject) {
        
        /** 设置页面跳转 */
        let app = UIApplication.sharedApplication();
        /** 指定根控制器 */
        app.keyWindow?.rootViewController = WlisTabBarC()
        /** 结束定时器 */
        self.timer?.invalidate();
    }
}

private extension WlisADVC{

    /** 懒加载数据方法 */
    @objc func tap(){
        let app = UIApplication.sharedApplication()
        
        /** 解析url */
        guard let url = NSURL(string: (self.item?.ori_curl)!)else{
        return
        }
        /** 判断url是否为空 */
        if app.canOpenURL(url){
            /** 打开 */
        app.openURL(url)
        }
    }
    
    
    /** 定义静态变量 */
    static var temp_i = 2;
    /** 实现定时器方法 */
    @objc func timeChange(){
        /**  */
        if WlisADVC.temp_i < 0{
            self.jumpBtn(UIButton());
        }
        /** 拼接字符串 */
        let str = "跳过" + "(\(WlisADVC.temp_i))";
        /** 设置文字 */
        self.jumpBtn.setTitle(str, forState: .Normal);
        WlisADVC.temp_i -= 1;
    }
    
    
    
    /** 设置启动图片 */
    func setUpLaunchImage() {
        
        if iphone7 {
            launchImage.image = UIImage(named: "LaunchImage-800-Portrait-736h.png")
        } else if iphone6 {
            launchImage.image = UIImage(named: "LaunchImage-800-667h@2x.png")
        } else if iphone5 {
            launchImage.image = UIImage(named: "LaunchImage-700-568h@2x.png")
        } else if iphone4 {
            launchImage.image = UIImage(named: "LaunchImage@2x.png")
        }
        
    }

    /** 加载网页数据 */
    func loadAdData(){
    
        /** 设置字典 */
        var parameters = [String : AnyObject]();
        parameters["code2"] = code2;
        
        /** 请求数据 */
        Alamofire.request(.GET, "http://mobads.baidu.com/cpro/ui/mads.php", parameters: parameters).responseJSON { (response) -> Void in
            if let JSON = response.result.value{
                /** 设置文件存储本地 */
//                (JSON as! NSDictionary).writeToFile("/Users/xmg/Desktop/xmg_UI/xmg_UI35day项目10/missScarlett/ad.plist", atomically: true);
                /** 框架装模型数据 */
                self.item = WlisAdItem.mj_objectWithKeyValues(JSON["ad"]!![0]);
                /** 设置广告的高度界面 */
                let h = WlisScreenW / self.item!.w * self.item!.h;
                /** 设置图片大小 */
                self.launchImage.frame = CGRectMake(0, 0, WlisScreenW, h);
                /** 设置图片 */
                self.launchImage.sd_setImageWithURL(NSURL(string: self.item!.w_picurl!));
                
            }
        }
    }
    
    
}
